/*************************************************
 *  Object:       Tric! A flying Ricotta.
 *  Authors:      Teo (matteo.piva-gmail.com)
                  Carletto (carlo.checchi-gmail.com)
*************************************************/

#include "config.h"
#include "util.h"


void Soft_UART_Write_Text(char* testo) {
    char cnt;
    for (cnt=0; cnt<strlen(testo); cnt++) {
        Soft_UART_Write(testo[cnt]);
    }
}

unsigned short UTIL_checkBattery() {
    unsigned short error;
    unsigned int vbatt;
    float normalizedVoltage;
    
    // Start conversion and wait until it ends
    ADCON0 = ADCON0|0x02;
    while (ADCON0&0x02) {}
    
    vbatt = ADRESL;
    vbatt |= (ADRESH << 8);
    
    normalizedVoltage = vbatt * UTIL_VbattConversionFactor;
    if (normalizedVoltage <= UTIL_minVbatt) error = 1;
    else error = 0;
    return error;
}

void UTIL_initCheckBattery() {
        // Select acquisition and conversion time
        ADCON2 = 0xBE;
        // Enable A/D module and wait for capacitor filling
        ADCON0 = ADCON0|0x01;
        // Need to wait almost 500us to permit capacitor to fill up
}